package designpatterns.iterator;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2/16/2022 7:52 PM
 */
public class Persons implements Container {

    public String[] names = {"张三", "李四", "王五"};

    @Override
    public Iterator getIterator() {
        return new NameIterator();
    }

    private class NameIterator implements Iterator {

        int index;

        NameIterator() {
        }

        @Override
        public boolean hasNext() {
            return index < names.length;
        }

        @Override
        public Object next() {
            if (hasNext()) {
                String s = names[index];
                index++;
                return s;
            }
            return null;
        }
    }
}
